#include <SFML/Graphics.hpp>
#include <iostream>
#include <stdlib.h>

#include "Unit.hpp"
#include "Drawing.h"
#include "GameWorld.hpp"
#include "Consts.h"





int main()
{
    int c_size = Constants::CASE_SIZE;
    int c_number = 21;
    GameWorld world(16,12);
    sf::RenderWindow window(sf::VideoMode(world.getMap().getSizeX() *c_size, world.getMap().getSizeY()*c_size , 32), "MasterTactics", sf::Style::Titlebar|sf::Style::Close);
    window.setVerticalSyncEnabled(true);

    Drawing drawmodule(&window);

    //Unit* selectedUnit = NULL;
    world.changeTerrainAt(10,2,3);
    world.changeTerrainAt(10,3,3);
    world.changeTerrainAt(10,4,3);

    world.changeTerrainAt(5,0,2);
    world.changeTerrainAt(5,1,2);
    world.changeTerrainAt(5,2,2);
    world.changeTerrainAt(5,3,2);
    world.changeTerrainAt(6,3,2);
    world.changeTerrainAt(6,4,2);
    world.changeTerrainAt(6,5,2);
    world.changeTerrainAt(7,5,2);
    world.changeTerrainAt(7,6,2);
    world.changeTerrainAt(8,6,2);
    world.changeTerrainAt(8,7,2);
    world.changeTerrainAt(9,7,2);
    world.changeTerrainAt(10,7,2);
    world.changeTerrainAt(11,7,2);
    world.changeTerrainAt(12,7,2);
    world.changeTerrainAt(13,7,2);
    world.changeTerrainAt(13,8,2);
    world.changeTerrainAt(14,8,2);
    world.changeTerrainAt(15,8,2);


    Player test_player_1 = Player(1,0,"koukoucjean");
    Player test_player_2 = Player(2,1,"sltcandresse");
    Player test_player_3 = Player(1,2,"suponsfeunpes");
    Player test_player_4 = Player(2,3,"sltcava");
    Player test_player_5 = Player(1,4,"slttttttt");
    Player test_player_6 = Player(2,5,"wouhaugo");
    Player test_player_7 = Player(1,6,"kelskineojourdui");
    Player test_player_8 = Player(1,7,"ninomiyakoun");
    world.addPlayer(test_player_1) ;
    world.addPlayer(test_player_2) ;
    world.addPlayer(test_player_3) ;
    world.addPlayer(test_player_4) ;
    world.addPlayer(test_player_5) ;
    world.addPlayer(test_player_6) ;
    world.addPlayer(test_player_7) ;
    world.addPlayer(test_player_8) ;

    Unit warrior(3, 10, 4, 1, world.getPlayer(0));
    Unit archer(3, 1, 4, 4, world.getPlayer(0));
    Unit priest(-5, 2, 3, 4, world.getPlayer(0));



    /*sf::Text txt("-123",Knewave,20); // Pour les tests
    txt.setColor(sf::Color::White);
    txt.setPosition(400,400);*/


    world.addUnit(warrior, sf::Vector2i(0, 0));

    warrior.setOwner(world.getPlayer(1));
    world.addUnit(warrior, sf::Vector2i(1, 1));

    warrior.setOwner(world.getPlayer(2));
    world.addUnit(warrior, sf::Vector2i(2, 2));

    warrior.setOwner(world.getPlayer(3));
    world.addUnit(warrior, sf::Vector2i(3, 3));

    warrior.setOwner(world.getPlayer(4));
    world.addUnit(warrior, sf::Vector2i(4, 4));

    warrior.setOwner(world.getPlayer(5));
    world.addUnit(warrior, sf::Vector2i(5, 5));

    warrior.setOwner(world.getPlayer(6));
    world.addUnit(warrior, sf::Vector2i(6, 6));

    warrior.setOwner(world.getPlayer(7));
    world.addUnit(warrior, sf::Vector2i(7, 7));

    double t = -500.0;

    Player * localplayer = world.getPlayingPlayer() ;

    while (window.isOpen())
    {
        sf::Event event;

        drawmodule.time_drawing += 0.05;

        sf::Vector2i mouse_pos = sf::Mouse::getPosition(window);
        int x = mouse_pos.x/c_size;
        int y = mouse_pos.y/c_size;

        if (x >= world.getMap().getSizeX())
            x = world.getMap().getSizeX() - 1 ;
        if (y >= world.getMap().getSizeY())
            y = world.getMap().getSizeY() - 1 ;

        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;

            default:
                break;
            }

            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {


                    Unit* target = world.getCursorUnit(x, y);

                    if (world.getSelected() == NULL)
                        world.select(x, y);
                    else if (target == NULL )
                    {
                        if (world.moveUnit(x,y,world.getSelected()) >= 1 ) // case deja prise ou hors de portee
                            world.unselect();
                    }
                    else if ( (world.getSelected()->getAttack()) && ((target->getOwner()->getTeam() != world.getSelected()->getOwner()->getTeam()) xor ( world.getSelected()->getAttack() <0 ) ) )
                    {
                        if (world.getSelected()->isInRange(target)  )
                        {
                            if (world.attackUnit(world.getSelected(),target) ) // l'attaquant est mort
                                world.unselect();

                        }

                    }
                    else
                    {
                        world.unselect();
                    }

                    if (world.getSelected() != NULL && !world.getSelected()->anyMoveAvalaible())
                        world.unselect() ;
                }
                if (event.mouseButton.button == sf::Mouse::Right)
                {


                    Unit* target = world.getCursorUnit(x, y);

                    if (world.getSelected() != NULL)
                    {
                        world.unselect();
                    }
                }
            }
            else if (event.type == sf::Event::KeyPressed)
            {

                /*if (event.key.code == sf::Keyboard::Space) // space allows to move this unit again
                {
                    if (world.getSelected() != NULL)
                    {
                        world.getSelected()->resetMove();
                    }
                }
                else*/
                if (event.key.code == sf::Keyboard::E) // ends turn
                {
                    std::cout << "turn of " << world.getPlayingPlayer()->getId() << " ended" << std::endl ;
                    world.endTurn(world.getPlayingPlayer());
                    localplayer = world.getPlayingPlayer() ;
                    std::cout << "turn of " << world.getPlayingPlayer()->getId() << " began" << std::endl ;
                }
            }
        }

        window.clear();

        world.hover(x,y);
        world.update(&drawmodule, localplayer);

        //textOnCase( 12,10,&window, txt, sf::Color::Black , 3, 16 )  ;


        //std::cout << txt.getGlobalBounds().top << " ; " << txt.getGlobalBounds().height << std::endl ;
        //std::cout << txt.getGlobalBounds().left << " , " << txt.getGlobalBounds().width << std::endl ;

        window.display();
    }

    return 0;
}
